import store from '@/store'
import {
  regionDataPlus,
  TextToCode,
} from "element-china-area-data";
import {
  selfarea
} from "@/api/house/broker"

/**
 * 字符权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkPermi(value) {
  if (value && value instanceof Array && value.length > 0) {
    const permissions = store.getters && store.getters.permissions
    const permissionDatas = value
    const all_permission = "*:*:*";

    const hasPermission = permissions.some(permission => {
      return all_permission === permission || permissionDatas.includes(permission)
    })

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
    return false
  }
}

/**
 * 角色权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkRole(value) {
  if (value && value instanceof Array && value.length > 0) {
    const roles = store.getters && store.getters.roles
    const permissionRoles = value
    const super_admin = "admin";

    const hasRole = roles.some(role => {
      return super_admin === role || permissionRoles.includes(role)
    })

    if (!hasRole) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like checkRole="['admin','editor']"`)
    return false
  }
}


/**
 * 检查自身区域权限
 * @param form 更新选中的form
 * @return {
 *  
 * }  返回选项，是否禁用，以及当前选项
 */
export async function checkcascaderAreaPers(form) {
  let selectedOptions = [];
  if (form.id != null) {
    //更新 (设置选中)
    let  data = {
      province: form.province,
      city: form.city,
      area: form.area,
    };
    let ops = [
      TextToCode[data.province].code,
    ];
    if (data.city != null) {
      ops.push(TextToCode[data.province][data.city].code);
      if (data.area != null) {
        ops.push(TextToCode[data.province][data.city][data.area].code);
      }
    }
    selectedOptions = ops;
  } 
 

  //设置可选选项
  let dialogAreaDisabled = false;
  let options = regionDataPlus;
  await selfarea().then(res => {
    let resResult = res.data;
     if(resResult==null){
      return;
     }
    // 配置不可选
    let province = resResult.province;
    let city = resResult.city;
    let area = resResult.area;
    if (city == "全部") {
      //province
      for (let i = 0; i < options.length; i++) {
        if (province != options[i].label) {
          options[i].disabled = true;
        }
      }
    }
    if (area == "全部") {
      for (let i = 0; i < options.length; i++) {
        if (province != options[i].label) {
          options[i].disabled = true;
        } else {

          for (let j = 0; j < options[i].children.length; j++) {
            if (city != options[i].children[j].label) {
              options[i].children[j].disabled = true;
            }

          }
        }
      }
    }

    if ((city != null && city != "全部") && (area != null && area != "全部")) {
      //直接禁用控件
      dialogAreaDisabled = true;
    }

    if(form.id==null){
      //新增，设置选中为自己权限内
      selectedOptions=getSelectOptions(resResult);
    }

  });


  return {
    selectedOptions,
    dialogAreaDisabled,
    options,
  }
}


  /**
   * 获得选中的数据
   * @param {*} form 
   * @returns 
   */
   function getSelectOptions(form){
    let  data = {
      province: form.province,
      city: form.city,
      area: form.area,
    };
    let ops = [
      TextToCode[data.province].code,
    ];
    if (data.city != null) {
      ops.push(TextToCode[data.province][data.city].code);
      if (data.area != null) {
        ops.push(TextToCode[data.province][data.city][data.area].code);
      }
    }
    let selectedOptions = ops;
    return selectedOptions;
  }